package com.eazykitchen.controller;

import com.eazykitchen.entity.Collect;
import com.eazykitchen.entity.Paginating;
import com.eazykitchen.service.CollectService;
import com.eazykitchen.util.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/food")
public class CollectController {

    Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CollectService collectService;

    @PostMapping("/addFood")
    public ResponseEntity<BaseResponse> addFood(@RequestBody Collect collect)throws Exception{
        try {
            int flag = collectService.addFood(collect);
            if (flag ==1){
                return BaseResponse.generateOKResponseEntity("菜品添加成功", "");
            }else {
                return BaseResponse.generateBadResponseEntity(500, "添加失败"+"error", "");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.debug("CollectController-->>addFood", e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @DeleteMapping
    public ResponseEntity<BaseResponse> delFood(@RequestBody Collect collect)throws Exception{
        try {
            int flag = collectService.delFood(collect.getId());
            if (flag == 1){
                return BaseResponse.generateOKResponseEntity("删除成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "删除失败", "");            }
        }catch (Exception e){
            e.printStackTrace();
            log.debug("CollectController-->>delFood", e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @PutMapping
    public ResponseEntity<BaseResponse> update(@RequestBody Collect collect)throws Exception{
        try {
            int flag = collectService.updateFood(collect);
            if (flag == 1) {
                return BaseResponse.generateOKResponseEntity("修改成功", "");
            } else {
                return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.debug("CollectController-->>update", e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @GetMapping
    public ResponseEntity<BaseResponse<Paginating<Collect>>> getFoodList(Collect collect) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            //获取列表
            Paginating<Collect> paginating=collectService.getFoodList(collect);
            return BaseResponse.generateOKResponseEntity(paginating);
        } catch (Exception e) {
            log.debug("获取列表错误"+e);
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<BaseResponse<Collect>> getUserInfo(@PathVariable("id") int id) {
        try {
            Collect collect=collectService.getFoodById(id);
            return BaseResponse.generateOKResponseEntity(collect);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(500, "服务器内部错误", "");
        }
    }
}
